import javax.swing.*;
import java.awt.*;
import java.awt.geom.Ellipse2D;

class CircleGUI {
    public static void showCircleWindow() {
        JFrame frame = new JFrame("圆形计算器");
        frame.setSize(500, 400);
        frame.setLayout(new BorderLayout());

        // 输入面板
        JPanel inputPanel = new JPanel(new GridLayout(2, 2, 10, 10));
        inputPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        JLabel radiusLabel = new JLabel("半径:");
        JTextField radiusField = new JTextField();
        JButton calculateBtn = new JButton("计算");
        JButton drawBtn = new JButton("绘制图形");

        inputPanel.add(radiusLabel);
        inputPanel.add(radiusField);
        inputPanel.add(calculateBtn);
        inputPanel.add(drawBtn);

        // 结果面板
        JTextArea resultArea = new JTextArea();
        resultArea.setEditable(false);
        resultArea.setFont(new Font("微软雅黑", Font.PLAIN, 14));

        calculateBtn.addActionListener(e -> {
            try {
                double radius = Double.parseDouble(radiusField.getText());
                double area = Math.PI * radius * radius;
                double perimeter = 2 * Math.PI * radius;

                String result = String.format("圆形计算结果:\n半径: %.2f\n面积: %.2f\n周长: %.2f",
                        radius, area, perimeter);
                resultArea.setText(result);
            } catch (NumberFormatException ex) {
                JOptionPane.showMessageDialog(frame, "请输入有效的数字！");
            }
        });

        drawBtn.addActionListener(e -> {
            try {
                double radius = Double.parseDouble(radiusField.getText());
                // 打开单独的绘图窗口
                new CircleDrawingWindow(radius);
            } catch (NumberFormatException ex) {
                JOptionPane.showMessageDialog(frame, "请输入有效的数字！");
            }
        });

        frame.add(inputPanel, BorderLayout.NORTH);
        frame.add(new JScrollPane(resultArea), BorderLayout.CENTER);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

class CircleDrawingWindow extends JFrame {
    private double radius;

    public CircleDrawingWindow(double radius) {
        this.radius = radius;
        initializeUI();
    }

    private void initializeUI() {
        setTitle("圆形图形 - 半径: " + radius);
        setSize(500, 500);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);

        CircleDrawingPanel drawingPanel = new CircleDrawingPanel(radius);
        add(drawingPanel);

        setVisible(true);
    }
}

class CircleDrawingPanel extends JPanel {
    private double circleRadius;

    public CircleDrawingPanel(double radius) {
        this.circleRadius = radius;
        setBackground(Color.WHITE);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;

        // 启用抗锯齿
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        if (circleRadius > 0) {
            g2d.setColor(Color.BLUE);
            g2d.setStroke(new BasicStroke(3));

            // 绘制圆形
            int centerX = getWidth() / 2;
            int centerY = getHeight() / 2;
            int diameter = (int) (circleRadius * 15); // 缩放以便显示

            Ellipse2D circle = new Ellipse2D.Double(centerX - diameter/2, centerY - diameter/2, diameter, diameter);
            g2d.draw(circle);

            // 绘制半径
            g2d.setColor(Color.RED);
            g2d.setStroke(new BasicStroke(2));
            g2d.drawLine(centerX, centerY, centerX + diameter/2, centerY);

            // 绘制标注
            g2d.setFont(new Font("微软雅黑", Font.BOLD, 14));
            g2d.drawString("半径: " + circleRadius, centerX + diameter/2 + 5, centerY);
            g2d.drawString("圆心", centerX - 15, centerY - 5);

            // 绘制圆心点
            g2d.fillOval(centerX - 3, centerY - 3, 6, 6);
        }
    }
}